package com.touch.app.common.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;

import java.nio.charset.StandardCharsets;
import java.util.Map;

public class HttpUtils {

    private HttpUtils(){}

    /**
     * 发起POST请求，请求数据为JSON
     * @param url 请求地址
     * @param headers 请求头
     * @param json 请求数据
     */
    public static String post(String url, Map<String, String> headers, String json) {
        HttpRequest post = HttpUtil.createPost(url);
        post.addHeaders(headers);
        post.setConnectionTimeout(15000);
        post.charset(StandardCharsets.UTF_8);
        post.body(json, "application/json");
        int status = 500;
        String response = null;
        try (HttpResponse execute = post.execute()) {
            status = execute.getStatus();
            response = execute.body();
        }
        if (status == 200) {
            return response;
        }else {
            return response;
        }
    }

}
